package com.alex.media;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

public class AlbumActivity extends Activity {
	private int[] _ids;
	private String[]_titles;
	private ListView listview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = this.getIntent();
		String albumName;
		albumName = intent.getExtras().getString("albums");
		listview = new ListView(this);
		Cursor c = this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[]{MediaStore.Audio.Media.TITLE,
				MediaStore.Audio.Media.DURATION,
				MediaStore.Audio.Media.ARTIST,
				MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.DISPLAY_NAME}, 
				MediaStore.Audio.Media.ALBUM + "='" + albumName+"'", 
				null,
				null);
	   c.moveToFirst();
	   _ids = new int[c.getCount()];
	   _titles = new String[c.getCount()];
	   for(int i=0;i<c.getCount();i++){
	       _ids[i] = c.getInt(3);
	       _titles[i] = c.getString(0);
	       c.moveToNext();
	  }
	   listview.setAdapter(new MusicListAdapter(this, c));
	   listview.setOnItemClickListener(new ListItemClickListener());
	   LinearLayout list = new LinearLayout(this);
	   list.setBackgroundResource(R.drawable.listbg);
	   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
	   LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	   list.addView(listview,params);
	   setContentView(list);
	   
	    
	}
	 class ListItemClickListener implements OnItemClickListener{

	    	@Override
	    	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
	    		// TODO Auto-generated method stub
	    		Intent intent = new Intent(AlbumActivity.this,MusicActivity.class);
	    		intent.putExtra("_ids", _ids);
	    		intent.putExtra("_titles", _titles);
	    		intent.putExtra("position", position);
	    		startActivity(intent);
	    		finish();
	    	}
	    	
	    }
}
